package com.jumi.microservice.material.controller;

import cn.hutool.http.HttpResponse;
import com.jumi.common.log.annotation.Log;
import com.jumi.common.log.enums.BusinessType;
import com.jumi.microservice.common.base.BaseController;
import com.jumi.microservice.common.base.dto.ResponseResult;
import com.jumi.microservice.common.constant.RequestConstant;
import com.jumi.microservice.common.excel.utils.ExcelUtils;
import com.jumi.microservice.common.exception.ExceptionSupply;
import com.jumi.microservice.common.page.TableDataInfo;
import com.jumi.microservice.material.dto.JmGoodsRequest;
import com.jumi.microservice.material.dto.JmGoodsResponse;
import com.jumi.microservice.material.dto.JmMaterialResponse;
import com.jumi.microservice.material.service.IJmGoodsService;
import com.jumi.microservice.material.service.JmGoodsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author ssk
 * @create 2020/8/5 11:00
 **/
@RestController
@RequestMapping("/goods")
@Api(tags = {"【基础资料】产品资料接口"})
@Validated
@Service
public class JmGoodsController extends BaseController implements IJmGoodsService {


    @Resource
    private JmGoodsService jmGoodsService;


    @PostMapping("/add")
    @ApiOperation("添加产品")
    @Log(title = "供应链管理-基础管理-产品资料", businessType = BusinessType.INSERT)
    public ResponseResult add(@RequestBody JmGoodsRequest jmGoodsRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        jmGoodsRequest.setCreateBy(createBy);
        int rows = jmGoodsService.save(jmGoodsRequest);
        return ResponseResult.success("操作成功!", jmGoodsRequest.getGoodsId());
    }

    @PostMapping("/edit")
    @ApiOperation("编辑产品")
    @Log(title = "供应链管理-基础管理-产品资料", businessType = BusinessType.UPDATE)
    public ResponseResult edit(@RequestBody JmGoodsRequest jmGoodsRequest, @RequestHeader(RequestConstant.ADMIN_NAME) String updateBy) {
        jmGoodsRequest.setUpdateBy(updateBy);
        int rows = jmGoodsService.update(jmGoodsRequest);
        return ResponseResult.success("操作成功!", jmGoodsRequest.getGoodsId());
    }

    @PostMapping("/deleteBatch")
    @ApiOperation("批量删除")
    @ApiImplicitParam(name = "goodsIds", value = "产品ID集合", required = true)
    @Log(title = "供应链管理-基础管理-产品资料", businessType = BusinessType.DELETE)
    public ResponseResult deleteBatch(@NotNull(message = "不能为空") Long[] goodsIds) {
        jmGoodsService.deleteBatch(goodsIds);
        return ResponseResult.success();
    }

    @GetMapping("/findById")
    @ApiOperation("根据ID查询产品")
    @ApiImplicitParam(name = "goodsId", value = "产品ID", required = true)
    public ResponseResult<JmGoodsResponse> findById(@NotNull(message = "不能为空") Long goodsId) {
        JmGoodsResponse jmGoods = jmGoodsService.findById(goodsId);
        return ResponseResult.success(jmGoods);
    }


    @GetMapping("/findByPage")
    @ApiOperation("分页查询")
    public TableDataInfo<JmGoodsResponse> findByPage(JmGoodsRequest jmGoodsRequest) {
        startPage();
        List<JmGoodsResponse> list = jmGoodsService.findAllDetail(jmGoodsRequest);
        return getDataTable(list);
    }


    @GetMapping("/export")
    @ApiOperation("导出excel表格")
    @Log(title = "供应链管理-基础管理-产品资料", businessType = BusinessType.EXPORT)
    public void export(JmGoodsRequest jmGoodsRequest,HttpServletResponse response){
        List<JmGoodsResponse> list = jmGoodsService.findAll(jmGoodsRequest);
        ExcelUtils<JmGoodsResponse> util = new ExcelUtils<JmGoodsResponse>(JmGoodsResponse.class);
        try {
            util.exportExcel(response,list, "产品资料_" + System.currentTimeMillis());
        } catch (IOException e) {
            throw new ExceptionSupply(500, "导出产品资料出错!").newException();
        }
    }

    @GetMapping("/importTemplate")
    @ApiOperation("生成导入模版")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtils<JmGoodsResponse> util = new ExcelUtils<>(JmGoodsResponse.class);
        try {
            util.importTemplateExcel(response, "产品资料模版");
        } catch (IOException e) {
            throw new ExceptionSupply(500, "模版生成出错").newException();
        }
    }

    @PostMapping("/importData")
    @ApiOperation("导入数据")
    @ApiImplicitParam(name = "file", value = "上传文件")
    public ResponseResult importData(@RequestBody MultipartFile file, @RequestHeader(RequestConstant.ADMIN_NAME) String createBy) {
        List<JmGoodsResponse> jmGoodsList = null;
        try {
            ExcelUtils<JmGoodsResponse> util = new ExcelUtils<>(JmGoodsResponse.class);
            jmGoodsList = util.importExcel(file.getInputStream());
	        jmGoodsList = jmGoodsList.stream().map(s -> s.format(s)).collect(Collectors.toList());
        } catch (Exception e) {
            throw new ExceptionSupply(500, "导入数据解析失败!").newException();
        }
        String message = jmGoodsService.importData(jmGoodsList, createBy);
        return ResponseResult.success(message);
    }


    @Override
    @ApiOperation("根据名称查询产品")
    @ApiImplicitParam(name = "goodsName", value = "产品名称")
    @GetMapping("/findAllByName")
    public ResponseResult<List<JmGoodsResponse>> findAllByName(String goodsName) {
        JmGoodsRequest jmGoodsRequest = new JmGoodsRequest();
        jmGoodsRequest.setGoodsName(goodsName);
        jmGoodsRequest.setStatus(0);
        List<JmGoodsResponse> list = jmGoodsService.findAllDetail(jmGoodsRequest);
        return ResponseResult.success(list);
    }



    @GetMapping("/findMaterialsDetail")
    @ApiOperation("根据产品Id及仓库Id获取物料详情(包含库存)")
    public ResponseResult<List<JmMaterialResponse>> findMaterialsByGoodsId(Long goodsId, Long warehouseId) {
        List<JmMaterialResponse> list = jmGoodsService.findMaterialsDetail(goodsId, warehouseId);
        return ResponseResult.success(list);
    }
}
